<template>
  <div class="mod-dishes-dishes">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.spuName" placeholder="菜品名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <!--一个下拉框-->
        <el-select v-model="dataForm.class" placeholder="菜品分类" clearable>
          <!--一个单独的选项，名为全部，值为空-->
          <el-option label="全部" value="0"></el-option>
          <el-option v-for="item in classes" :key="item.classifyId" :label="item.classifyName"
            :value="item.classifyId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle(0)">新增</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <el-button type="primary" @click="$router.push({ name: 'classification' })">管理分类</el-button>
      </el-form-item>
    </el-form>
    <!--一个分页表单-->
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="spuId" header-align="center" align="center" label="SpuID">
      </el-table-column>
      <el-table-column prop="spuImage" header-align="center" align="center" label="商品图片">
        <template slot-scope="scope">
          <img :src="scope.row.spuImage" style="width: 100px; height: 100px;">
        </template>
      </el-table-column>
      <el-table-column prop="spuName" header-align="center" align="center" label="商品名">
      </el-table-column>
      <el-table-column prop="recommendReason" header-align="center" align="center" label="推荐理由">
      </el-table-column>
      <el-table-column prop="salesVolume" header-align="center" align="center" label="销量">
      </el-table-column>
      <el-table-column prop="classifyName" header-align="center" align="center" label="分类">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="230" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.spuId)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.spuId)">删除</el-button>
          <el-button type="text" size="small" @click="manageSku(scope.row.spuId)">管理规格</el-button>
          <el-button type="text" size="small" @click="manageRecipes(scope.row.spuId)">食谱管理</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

  </div>
</template>
<script>

export default {
  data() {
    return {
      dataForm: {
        spuName: '',
        class: ''
      },
      pageIndex: 1,
      pageSize: 10,
      totalPage: 1,
      dataListLoading: false,
      dataList: [],
      dataListSelections: [],
      classes: []
    }
  },
  activated() {
    this.getDataList()
    this.getClass()
  },
  methods: {
    getDataList() {
      this.$http({
        url: this.$http.adornUrl(`/web/spuController/list`),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'spuName': this.dataForm.spuName,
          'classifyId': this.dataForm.class,
        })
      }).then(({ data }) => {
        console.log(data)
        this.dataList = data.page.list
      })

    },
    getClass() {
      this.$http({
        url: this.$http.adornUrl(`/web/classfication/list`),
        method: 'get',
      }).then(({ data }) => {
        this.classes = data.list
      })
    },
    addOrUpdateHandle(id) {
      this.$router.push({ name: 'dishesaddupdate', params: { id } });
    },
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.spuId
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/web/spuController/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => { })
    },
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    manageSku(id) {
      this.$router.push({ name: 'sku', params: { id } });
    },
    sizeChangeHandle() {

    },
    currentChangeHandle() {

    },
    manageRecipes(id) {
      this.$router.push({ name: 'recipes', params: { id } });
    }
  }
}
</script>
<style></style>